<template>
  <div class="wrapper">
    <el-card class="cover" v-if="loginAdmin.id">
      <slide-verify :l="42"
                    :r="10"
                    :w="310"
                    :h="155"
                    slider-text="向右滑动"
                    @success="onSuccess"
                    @fail="onFail"
                    @refresh="onRefresh"
      ></slide-verify>
    </el-card>
    <div id="building">

      <div class="box">
        <img src="@/assets/logo.png" alt="" style="width: 40px; height: 40px;  top: 10px; left: 20px">
        <div style="font-size: 20px; margin-top: 5px; color:dodgerblue; font-weight: bold;">九州记图书管理系统</div>
      </div>

      <div>
        <div style="margin: 20px; text-align: center; font-size: 30px; font-weight: bold; color: dodgerblue">登 录</div>
        <el-form :model="admin" :rules="rules" ref="loginForm">
          <el-form-item prop="username">
            <el-input placeholder="请输入账号" prefix-icon="el-icon-user" size="medium" v-model="admin.username"></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input placeholder="请输入密码" show-password prefix-icon="el-icon-lock" size="medium" v-model="admin.password"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button style="width: 100%" size="medium" type="primary" @click="login">登录</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
import request from "@/utils/request";
import Cookies from 'js-cookie'

export default {
  name:  'LOGIN',
  data() {
    return {
      loginAdmin: {},
      admin: {},
      rules: {
        username: [
          {required: true, message: '请输入用户名', trigger: 'blur'},
          {min:3 ,  max:10 ,message: '长度在3到10个字符', trigger:'blur'},
        ],
        password:[
          {required: true, message: '请输入密码', trigger: 'blur'},
          {min:3 ,  max:10 ,message: '长度在3到10个字符', trigger:'blur'},
        ]
      }

    }
  },
  methods: {
    login() {
      this.$refs['loginForm'].validate((valid)=>{
        if(valid){
          request.post('/admin/login',this.admin).then(res=>{
            if (res.code === '200'){
              this.loginAdmin = res.data
            } else {
              this.$notify.error(res.msg);
            }
          })
        }
      })
    },
    onSuccess() {
      Cookies.set('admin', JSON.stringify(this.loginAdmin))
      this.$notify.success("登录成功")
      this.$router.push('/')
    },
    onFail() {

    },
    onRefresh() {
      console.log('refresh')
    }

  }
}
</script>

<style>
.wrapper {
  /*渐变的背景色*/
  /*height: 100vh;*/
  /*background-image: linear-gradient(to bottom right, #FC466B, #3F5EF8);*/
  /*overflow: hidden;*/
  /*背景图*/
  background-image: url("@/assets/图书馆.jpg");
  width: 100%;
  height: 100%;
  position: fixed;
  background-size: 100% 100%;
  /*position: relative;*/
}

#building {

  border:1px solid #dccfcf;
  width: 500px;
  height: 400px;
  border-radius: 10px;
  margin:200px auto;
  padding: 50px;

  -webkit-border-radius: 5px;
  -moz-border-radius: 5px;
  box-shadow: 0 0 25px #909399;
  background-color:rgba(255,255,255,0.7);


}

.cover {
  width: fit-content;
  background-color: white;
  position: absolute;
  top:50%;
  left:50%;
  transform: translate(-50%, -50%);
  z-index: 1000;
}

.box {
  width: 300px;
  height: 45px;
  display: flex;

}
</style>